home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / test / Ethernet / get_address.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  3.1 KB  |  110 lines

  1. /*
  2.  *  $Id: get_address.c,v 1.1.1.1 1999/05/18 15:33:43 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  get_address.c - Retrieve the MAC and IP address of an interface
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Redistribution and use in source and binary forms, with or without
  12.  * modification, are permitted provided that the following conditions
  13.  * are met:
  14.  * 1. Redistributions of source code must retain the above copyright
  15.  *    notice, this list of conditions and the following disclaimer.
  16.  * 2. Redistributions in binary form must reproduce the above copyright
  17.  *    notice, this list of conditions and the following disclaimer in the
  18.  *    documentation and/or other materials provided with the distribution.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  21.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  24.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30.  * SUCH DAMAGE.
  31.  *
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../../include/config.h"
  36. #endif
  37. #include "../libnet_test.h"
  38.  
  39. int
  40. main(int argc, char *argv[])
  41. {
  42.     int  c;
  43.     char errbuf[256];
  44.     char *device = NULL;
  45.     struct link_int *l;
  46.     struct ether_addr *e;
  47.     u_long i;
  48.  
  49.     while ((c = getopt(argc, argv, "i:")) != EOF)
  50.     {
  51.         switch (c)
  52.         {
  53.             case 'i':
  54.                 device = optarg;
  55.                 break;
  56.             default:
  57.                 exit(EXIT_FAILURE);
  58.         }
  59.     }
  60.  
  61.     if (!device)
  62.     {
  63.         fprintf(stderr, "Specify a device\n");
  64.         exit(EXIT_FAILURE);
  65.     }
  66.  
  67.     l = libnet_open_link_interface(device, errbuf);
  68.     if (!l)
  69.     {
  70.         fprintf(stderr, "libnet_open_link_interface: %s\n", errbuf);
  71.         exit(EXIT_FAILURE);
  72.     }
  73.  
  74.     printf("Interface %s\n", device);
  75.     e = libnet_get_hwaddr(l, device,  errbuf);
  76.     if (!e)
  77.     {
  78.         fprintf(stderr, "Can't get hardware address: %s\n", errbuf);
  79.         exit(EXIT_FAILURE);
  80.     }
  81.     else
  82.     {
  83.         printf("MAC address: ");
  84.         for (c = 0; c < 6; c++)
  85.         {
  86.             printf("%x", e->ether_addr_octet[c]);
  87.             if (c != 5)
  88.             {
  89.                 printf(":");
  90.             }
  91.         }
  92.         printf("\n");
  93.     }
  94.  
  95.     i = libnet_get_ipaddr(l, device, errbuf);
  96.     if (!i)
  97.     {
  98.         fprintf(stderr, "Can't get ip address: %s\n", errbuf);
  99.         exit(EXIT_FAILURE);
  100.     }
  101.     else
  102.     {
  103.         printf("IP  address: ");
  104.         printf("%s\n", libnet_host_lookup(ntohl(i), 0));
  105.     }
  106.     exit(EXIT_SUCCESS);
  107. }
  108.  
  109. /* EOF */
  110.